﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CommerceOne.Widgets.PagingAndSortingNav
{
    public class PagingSortingNavController
    {

        public PagingSortingNavViewModel GetViewModel(int totalRecord, int pageSize, int currentPage, string pageUrl)
        {
            PagingSortingNavViewModel viewModel = new PagingSortingNavViewModel();
            viewModel.PaginationList = new List<LinkItem>();
            viewModel.SortingList = new List<LinkItem>();

            int remain = 0;
            int totalPage =Math.DivRem(totalRecord, pageSize, out remain);
            if (remain > 0) totalPage++;

            for (int i = totalPage; i >=1; i--)
            {
                LinkItem linkItem = new LinkItem();
                linkItem.LinkText = i.ToString();
                linkItem.LinkUrl = string.Format(pageUrl, i);
                if (i == currentPage) linkItem.IsCurrentSelected = true;
                viewModel.PaginationList.Add(linkItem);
            }

            // TODO: adding sorting
            return viewModel;
        }
    }
}